<?php
/**
 * Created by PhpStorm.
 * User: bxm
 * Date: 2022/6/20
 * Time: 18:16
 */

namespace app\api\controller\worker;


use app\api\controller\Base;
use app\common\model\node\NodeModel;
use app\common\model\node\NodeProcessModel;
use app\common\model\worker\CustomerAuthModel;
use app\common\model\worker\CustomerChannelModel;
use app\common\model\worker\CustomerFollowLogModel;
use app\common\model\worker\CustomerModel;
use app\common\model\worker\TaskModel;
use DateTime;
use app\api\controller\worker\TaskController;
use think\Request;

class CustomerFollowLogController extends Base
{
    private $model;
    private $taskModel;
    /**
     * 构造方法
     * @access public
     * @throws \Exception
     */
    public function __construct()
    {
        parent::__construct();
        $this->checkLogin();
        $this->model = new CustomerFollowLogModel();
        $this->taskModel = new TaskModel();
    }
    public function add(){
        $roles_ids = $this->role_ids;
        foreach ($roles_ids as $v){
            if(!in_array($v,[6,7,8])){
                $this->result([],'该用户没有权限，只有设计师，业务员，客户代表有操作权限！！',0);
            }
        }
        //var_dump($this->loginInfo);exit;
        $data = $this->request->param();
        $this->validate($data,[
            'customer_id|客户id'=>'require',
            'follow_type|跟进方式'=>'require|number',
            'content|沟通内容'=>'require',
            'reply_content|结果'=>'require',
            'next_follow_time|下次跟进时间'=>'require',
            'next_content|下次跟进内容'=>'require',
        ]);
        if(!empty($data['follow_imgs'])){
            $data['follow_imgs'] = json_encode($data['follow_imgs'],JSON_UNESCAPED_UNICODE);
        }
        $data['action_admin_id'] = $this->loginInfo['id'];
        //todo 验证是否有编辑权限
        $info = $this->model->add($data);
//        $nodeProcess = NodeProcessModel::where('node_id', 2)->find();
//        $nodeInfo = NodeModel::where('id',2)->find();
//        if(empty($nodeInfo)) exception('任务配置已被修改');
//
//        //添加到任务里
//        $dataTask = [
//            'customer_id' => $data['customer_id'],
//            'order_id' => 0,
//            'node_id' => 2,
//            'node_type' => 1,
//            'node' => '跟进',
//            'process_id' => 4,
//            'process_name' => '跟进',
//            'process_status' => 1,//节点状态值
//            'process_content' =>$nodeProcess['process_config'],
//            //'receive_admin_id' => $receiveAdminId,//执行人
//            'receive_time' => '',//操作时间
//            'is_receive' => 0,//是否接收
//            'is_audit'=>0,//是否审核
//            'audit_status'=> 0,//审核状态-1退回重做 0待审核 1已审核
//            'audit_idea' => '',//审核意见
//            'audit_admin_id' =>$nodeProcess['audit_admin_id'],//节点审核人
//            'audit_time' => '',//审核时间
//            'admin_id' => $this->loginInfo['id'],//任务发起人
//            'is_submit' => 0,//是否提交
//            'submit_time'=> '',//提交时间
//            'create_time'=> date('Y-m-d H:i:s'),
//            'update_time' => date('Y-m-d H:i:s'),
//            'audit_admin_ids'=> $nodeProcess['audit_admin_id'],//审核人ids
//            'deadline_time' => '',//截止日期
//            'task_file' => '',//任务附件
//            'copy_admin_ids' => $nodeProcess['copy_admin_id'],//抄送人
//            'task_imgs'=> json_encode($data['follow_imgs'],JSON_UNESCAPED_UNICODE),//任务图片
//        ];
//        $this->taskModel->commonAddTask($dataTask);
        $this->result($info);
    }
    public function index(){
        $data = $this->request->param();
        $customer_id  = $data['customer_id'];
        $pageSize      = $this->request->param('pageSize/d', 20); //每页多少条
        $pageNumber    = $this->request->param('pageNumber/d') ?? 1; //当前页

        $list = $this->model->alias('cal')
            ->leftJoin('admin a', 'a.id = cal.action_admin_id')
            ->where('cal.customer_id',$customer_id)
            ->order('cal.id','desc')
            ->field('a.nickname,cal.*')
             ->paginate(['list_rows'=> $pageSize, 'page' => $pageNumber]);



        $this->result($list,'操作成功',1);
    }
    public function getInfo(Request $request)
    {
        $param = $request->param();
        //开始校验
        $this->validate($param, [
            'id|记录id' => 'require',
        ]);
        $id = $param['id'];
        $list = $this->model->alias('cal')->leftJoin('admin a', 'a.id = cal.action_admin_id')
            ->where('cal.id', $id)
            ->field('a.nickname,cal.*')
            ->find();
        $this->result($list, '操作成功', 1);
    }
    /**
     * @author bxm
     * @date 2022-06-20 19:03
     * 获取最近一个跟进记录
     */
    public function getInfo100(){
        $data = $this->request->param();
        $customer_id  = $data['customer_id'];
        $CustomerModel = new CustomerModel();
//        $info = $CustomerModel->getInfo(['id'=>$customer_id],'id','business_name','name','mobile','house_estate',
//                'address','channel_id','recommend_id','customer_pain_content');
        $info = CustomerModel::where('id',$customer_id)
            ->field('id,business_name,name,mobile,house_estate,
                address,channel_id,recommend_id,customer_pain_content')->find()->toArray();
        $customerChannelModel = new CustomerChannelModel();

        $customerChanneList = $customerChannelModel->getList(['status'=>1]);
        $arr = array_column($customerChanneList,'name','id');
        $info['channel_name'] = $arr[$info['channel_id']] ?? '';


        //最近一次得跟进记录
        $CustomerFollowLogInfo = CustomerFollowLogModel::order('id','desc')->find();
        if($CustomerFollowLogInfo){
            $date1 = new DateTime("now");

            $date2 = new DateTime($CustomerFollowLogInfo['create_time']);

            $interval = $date1->diff($date2);
            $day_diff = $interval->d;

        }

        $CustomerFollowLogInfo['day_diff'] = $day_diff ?? '';

        $info['follow_at'] = $CustomerFollowLogInfo;//


        $this->result($info,'操作成功',1);
    }


}